package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;
    @Autowired
    private DistrictInfoMapper districtInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private CityInfoMapper cityMapper;

    @Override
    @Transactional
    public void saveOrUpdateItem(ApartmentSubmitVo apartmentSubmitVo) {
        //1.如果用户执行的是更新操作 则先删除中间表数据.
        if (apartmentSubmitVo.getId() != null) {
            //1.删除图片信息
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId())
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(lambdaQueryWrapper);

            //2.删除公寓的配套信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //3.删除标签关联表数据
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId()));

            //4.删除公寓杂费信息
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId()));
        }


        //1.查询省市区的名称
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        CityInfo cityInfo = cityMapper.selectById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfo.getName());

        DistrictInfo districtInfo = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //apartmentSubmitvo   apartmentInfo??
        //思路1:  通过Iget/set方法将vo中的值 转化到info中  无需特殊规则
        //思路2:  直接入库vo    需要有特殊关系  继承!!!
        //apartmentInfoMapper.insert(apartmentSubmitVo);
        this.saveOrUpdate(apartmentSubmitVo);
        Long apartmentId = apartmentSubmitVo.getId();

        //新增图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphList = new ArrayList<>(graphVoList.size());
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(apartmentId);
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphList.add(graphInfo);
        });
        //完成批量新增
        graphInfoService.saveBatch(graphList);


        //新增公寓的配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
        facilityInfoIds.forEach(facilityInfoId -> {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                    .apartmentId(apartmentId)
                    .facilityId(facilityInfoId).build();
            apartmentFacilityList.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(apartmentFacilityList);

        //新增标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIds.size());
        labelIds.forEach(labelId -> {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                    .apartmentId(apartmentId)
                    .labelId(labelId).build();
            apartmentLabelList.add(apartmentLabel);
        });
        apartmentLabelService.saveBatch(apartmentLabelList);

        //新增杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIds.size());
        feeValueIds.forEach(feeValueId -> {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                    .apartmentId(apartmentId)
                    .feeValueId(feeValueId).build();
            apartmentFeeValueList.add(apartmentFeeValue);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValueList);
    }

    @Override
    public IPage<ApartmentItemVo> findPageList(long current, long size, ApartmentQueryVo queryVo) {

        //自定义分页API
        IPage<ApartmentItemVo> page = new Page<>(current, size);
        page = apartmentInfoMapper.findPageList(page, queryVo);
        return page;
    }

    /**
     * 参数分析:
     * 1.id 公寓id号
     * 2.返回值  ApartmentDetailVo
     * 2.1 ApartmentInfo 公寓表信息
     * 2.2 graphVoList  图片信息
     * 2.3 labelInfoList 标签信息
     * 2.4 facilityInfoList 配套信息
     * 2.5 feeValueVoList 杂费信息
     **/
    @Override
    public ApartmentDetailVo findDetailById(Long id) {
        //1.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //2.将公寓信息 赋值给VO
        ApartmentDetailVo detailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, detailVo);

        //3.查询公寓的图片信息   Sql  MP
        List<GraphVo> graphVoList = graphInfoService.findGraphInfoByItemId(id, ItemType.APARTMENT);
        detailVo.setGraphVoList(graphVoList);

        //4.查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.findLabelInfoListByApartmentId(id);
        detailVo.setLabelInfoList(labelInfoList);

        //5.查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findFacilityInfoByApartmentId(id);
        detailVo.setFacilityInfoList(facilityInfoList);

        //6.查询杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.findFeeValueByApartmentId(id);
        detailVo.setFeeValueVoList(feeValueVoList);

        return detailVo;
    }

    @Override
    public void removeApartmentById(Long id) {

        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //2.删除ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelQueryWrapper);

        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityQueryWrapper);

        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeQueryWrapper);

        //5.删除ApartmentInfo
        super.removeById(id);
    }
}




